package com.huaxinkaiye.lookbigphoto;

import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.provider.MediaStore;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;

import com.huaxinkaiye.R;
import com.huaxinkaiye.utils.ToastUtil;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

import java.util.ArrayList;

/**
 * Created by imac on 15/4/5.
 */
public class PhotosDialog extends Dialog {
    private TextView tvCount;
    private HackyViewPager hackyViewPager;
    private Context context;
    private PhotoZoomAdapter adapter;
    private ImageButton ibBack;
    private ImageButton ibSave;
    private String currentUrl;

    public PhotosDialog(Context context) {
        super(context);
        init(context);
    }

    public PhotosDialog(Context context, int theme) {
        super(context, theme);
        init(context);
    }

    protected PhotosDialog(Context context, boolean cancelable, OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
        init(context);
    }

    private void init(final Context context) {
        this.context = context;
        View view = LayoutInflater.from(context).inflate(R.layout.activity_detail_photo, null);
        tvCount = (TextView) view.findViewById(R.id.tv_count);
        hackyViewPager = (HackyViewPager) view.findViewById(R.id.gvp_photo);
        ibBack = (ImageButton) view.findViewById(R.id.ib_back);
        ibBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhotosDialog.this.dismiss();
            }
        });
        ibSave = (ImageButton) view.findViewById(R.id.ib_save);
        ibSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                saveImage();
            }
        });
        setContentView(view);
        getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
    }


    public void show(int currentIndex, final ArrayList<String> urls) {
        if (urls == null)
            return;
        tvCount.setText((currentIndex + 1) + "/" + urls.size());
        currentUrl = urls.get(currentIndex);
        adapter = new PhotoZoomAdapter(context, urls);
        hackyViewPager.setAdapter(adapter);
        hackyViewPager.setCurrentItem(currentIndex);

        hackyViewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int i) {
                tvCount.setText((i + 1) + "/" + urls.size());
                currentUrl = urls.get(i);
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        show();
    }

    private void saveImage() {
        if (currentUrl == null) {
            ToastUtil.show(context, "保存图片失败");
            return;
        }
        ImageLoader.getInstance().loadImage(currentUrl, new MyImageLoadingListener());
    }

    public class MyImageLoadingListener implements ImageLoadingListener {
        @Override
        public void onLoadingStarted(String s, View view) {

        }

        @Override
        public void onLoadingFailed(String s, View view, FailReason failReason) {

        }

        @Override
        public void onLoadingComplete(String s, View view, Bitmap bitmap) {
            //保存图片
            if (bitmap == null) {
                ToastUtil.show(context, "保存失败");
                return;
            }
            MediaStore.Images.Media.insertImage(context.getContentResolver(), bitmap, getPhotoName()+ "", "");
            ToastUtil.show(context, "保存成功");
        }

        @Override
        public void onLoadingCancelled(String s, View view) {

        }
    }

    //获取图片名称
    private String getPhotoName() {
        if (currentUrl == null)
            return System.currentTimeMillis() + "";
        return currentUrl.substring(currentUrl.lastIndexOf("/")+1);
    }
}
